var CaseSelectionUpload = {
    id: "caseSelectionUploadTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

CaseSelectionUpload.initColumn = function () {
    var columns =
        [
            {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {
                title: '状态', field: 'saveStatus', align: 'center', valign: 'middle', width: 110, sortable: true,
                formatter: function (value, row, index) {
                    if (value == 0) {
                        return "待提交";
                    } else {
                        return "已提交"
                    }
                }
            },
            {title: '病例类型', field: 'caseType', align: 'center', valign: 'middle', width: 110, sortable: true},
            {
                title: '姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
                formatter: function (value, row, index) {
                    if (row.customerAge == "" || row.customerAge == null || row.customerAge == "null") {
                        return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    } else if (row.customerAge >= 0 && row.customerAge < 18) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/nan-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/nv-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    } else if (row.customerAge >= 18 && row.customerAge <= 50) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-adult.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/lfemale-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    } else if (row.customerAge > 50) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/male-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    }
                }
            },
            {
                title: '性别', field: 'customerSex', align: 'center', valign: 'middle', width: 80, sortable: true,
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "男";
                    } else if (value == 2) {
                        return "女"
                    } else {
                        return "未知"
                    }
                }
            },
            {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80},
            {
                title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true, width: 170,
                formatter: function (value, row, index) {
                    return value.substring(0, 19);
                }
            },
            {
                title: '申请时间', field: 'applyTime', align: 'center', valign: 'middle', sortable: true, width: 170,
                formatter: function (value, row, index) {
                    return value.substring(0, 19);
                }
            },
            {
                title: '退回时间', field: 'backTime', align: 'center', valign: 'middle', sortable: true, width: 170,
                formatter: function (value, row, index) {
                    return value.substring(0, 19);
                }
            },
            {
                title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 170,
                formatter: function (value, row, index) {
                    return '<span title="' + value + '">' + value + '</span>'
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
                }
            },
            {
                title: '退回说明', field: 'backedRemark', align: 'center', valign: 'middle', sortable: true, width: 250,
                formatter: function (value, row, index) {
                    return '<span title="' + value + '">' + value + '</span>'
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
                }
            },
            {
                title: '操作', field: 'caozuo', align: 'center', valign: 'middle', width: 60,
                formatter: function (value, row, index) {
                    if (row.saveStatus == 0) {
                        return '<button title="修改病例" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="CaseSelectionUpload.updateBodyPulpCase(' + "'" + row.id + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-pencil"></span></button>' +
                            '<button title="删除病例" onclick="CaseSelectionUpload.deleteBodyPulpCase(\'' + row.id + '\',\'' + row.customerName + '\')"" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-trash"></span></button>'
                    } else {
                        return '<button title="查看病例" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="CaseSelectionUpload.seeBodyPulpCase(' + "'" + row.id + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-eye-open"></span></button>'
                    }
                }
            }
        ];
    return columns;
};

CaseSelectionUpload.search = function () {//查询预约客户列表
    CaseSelectionUpload.table.refresh({query: CaseSelectionUpload.formParams()});
    CaseSelectionUpload.statistics();
};

CaseSelectionUpload.formParams = function () {//新市场预约表单查询提交参数
    var queryData = {};
    queryData['reach'] = $("#caseSelectionPublic", parent.document).val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['isUpload'] = $("#isUpload").val();
    return queryData;
};

CaseSelectionUpload.statistics = function () {//统计建档未到人数
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + '/caseSelectionUpload/statistics',
        data: CaseSelectionUpload.formParams(),
        dataType: "json",
        success: function (data) {
            $("#daitj").html(data[0].countNum);
            $("#yitj").html(data[1].countNum);
            $("#thjl").html(data[2].countNum);
        }
    });
};

var parseParam = function (param) {//把对象拼接成URL的键值对参数 param1=value1&param2=value2
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};

var getSingleData = function (param, key) {
    return key + "=" + param[key];
};

CaseSelectionUpload.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        CaseSelectionUpload.seItem = selected[0];
        return true;
    }
};

CaseSelectionUpload.resetSearch = function () {//重置
    $("#caseSelectionPublic", parent.document).val("");
    $("#startTime").val("");
    $("#endTime").val("");
    CaseSelectionUpload.search();
};

CaseSelectionUpload.uploadWaiting = function () {
    document.getElementById("isUpload").value = "0";
    $('#caseSelectionUploadTable').bootstrapTable('hideColumn', 'applyTime');
    $('#caseSelectionUploadTable').bootstrapTable('hideColumn', 'backTime');
    $('#caseSelectionUploadTable').bootstrapTable('hideColumn', 'backedRemark');
    $('#caseSelectionUploadTable').bootstrapTable('showColumn', 'caozuo');
    CaseSelectionUpload.search();
    $("#uploadWaiting").addClass('actionClick').siblings().removeClass('actionClick');
};

CaseSelectionUpload.uploaded = function () {
    document.getElementById("isUpload").value = "1";
    $('#caseSelectionUploadTable').bootstrapTable('showColumn', 'applyTime');
    $('#caseSelectionUploadTable').bootstrapTable('hideColumn', 'backTime');
    $('#caseSelectionUploadTable').bootstrapTable('hideColumn', 'backedRemark');
    $('#caseSelectionUploadTable').bootstrapTable('showColumn', 'caozuo');
    CaseSelectionUpload.search();
};

CaseSelectionUpload.gobackrecrod = function () {
    document.getElementById("isUpload").value = "2";
    $('#caseSelectionUploadTable').bootstrapTable('hideColumn', 'caozuo');
    $('#caseSelectionUploadTable').bootstrapTable('showColumn', 'applyTime');
    $('#caseSelectionUploadTable').bootstrapTable('showColumn', 'backTime');
    $('#caseSelectionUploadTable').bootstrapTable('showColumn', 'backedRemark');
    CaseSelectionUpload.search();
};

CaseSelectionUpload.addBodyPulpCase = function () {//新建牙体牙髓病例 @param id
    var index = layer.open({
        type: 2,
        title: '新建牙体牙髓病例',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/caseSelectionUpload/to_body_pulp_case_add/'
    });
    this.layerIndex = index;
};

CaseSelectionUpload.seeBodyPulpCase = function (id) {//查看已提交牙体牙髓病例 @param id
    var index = layer.open({
        type: 2,
        title: '查看牙体牙髓病例',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/caseSelectionUpload/to_body_pulp_case_view/' + id
    });
    this.layerIndex = index;
};

CaseSelectionUpload.updateBodyPulpCase = function (id) {//修改牙体牙髓病例
    var index = layer.open({
        type: 2,
        title: '新建牙体牙髓病例',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/caseSelectionUpload/to_body_pulp_case_update/' + id
    });
    this.layerIndex = index;
};

CaseSelectionUpload.deleteBodyPulpCase = function (id, customerName) {//删除牙体牙髓病例
    var r = confirm("是否删除患者：" + customerName + " 的病例？")
    if (r == true) {
        var ajax = new $ax(Feng.ctxPath + "/caseSelectionUpload/deleteBodyPulpCase/" + id, function (data) {
            Feng.success("删除成功!");
            ReservationsProject.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
};

$("#caseSelectionSearchAll", parent.document).click(function () {//主页搜索
    CaseSelectionUpload.search();
});

CaseSelectionUpload.time = function () {//初始化赋值时间
    var date = new Date();
    var currentYear = date.getFullYear();
    var currentMonth = date.getMonth() + 1;
    var currentDay = date.getDate();
    if (currentMonth < 10) currentMonth = "0" + currentMonth;
    if (currentDay < 10) currentDay = "0" + currentDay;
    var dates = new Date();
    dates.setDate(dates.getDate() - 1);
    var currentYears = dates.getFullYear();
    var currentMonths = dates.getMonth() + 1;
    var currentDays = dates.getDate();
    if (currentMonths < 10) currentMonths = "0" + currentMonths;
    if (currentDays < 10) currentDays = "0" + currentDays;
    $("#startTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + "00:00:00");
    $("#endTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + "23:59:59");
};

var scollPostion = 0;
CaseSelectionUpload.getScrollPosition = function () {//获取表格滚动到什么地方
    scollPostion = $('#CaseSelectionUpload').bootstrapTable('getScrollPosition');
};

CaseSelectionUpload.scrollTo = function () {//指定表格滚动到什么地方
    $("#CaseSelectionUpload").on('load-success.bs.table', function (data) {
        $('#CaseSelectionUpload').bootstrapTable('scrollTo', scollPostion);
    });
};

$(function () {
    var defaultColunms = CaseSelectionUpload.initColumn();
    var table = new BSTable(CaseSelectionUpload.id, "/caseSelectionUpload/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(CaseSelectionUpload.formParams());
    CaseSelectionUpload.table = table.init();
    CaseSelectionUpload.uploadWaiting();
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {CaseSelectionUpload.search();});
    checkDate = function () {CaseSelectionUpload.search();};
    callbackTable = function () {CaseSelectionUpload.search();};
});